home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / filesyst / thsfs.tgz / thsfs.tar / thsfs / dir.c next >
C/C++ Source or Header  |  1994-10-04  |  5KB  |  244 lines

  1. /*************************************************************
  2.  *                                                           *
  3.  *    ths  Filesystem                  04.10.94      V1.1    *
  4.  *                                                           *
  5.  *    Thomas Scheuermann     ths@ai-lab.fh-furtwangen.de     *
  6.  *                                                           *
  7.  *************************************************************/
  8.  
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/stat.h>
  15. #include <linux/mm.h>
  16. #include <linux/locks.h>
  17. #include <linux/fs.h>
  18. #include <linux/malloc.h>
  19.  
  20. #include <asm/system.h>
  21. #include <asm/segment.h>
  22. #include <asm/bitops.h>
  23.  
  24. #include "ths.h"
  25. #include "ths_i.h"
  26.  
  27. int ths_dir_read(struct inode *in, struct file *fp, char *buf, int count)
  28. {
  29.     printk("dir_read\n");
  30.     return -EISDIR;
  31. }
  32.  
  33. int ths_readdir(struct inode *in, struct file *fp,struct dirent *eintrag, int count)
  34. {
  35.     struct ths_buffer tbf;
  36.     struct ths_inode_info *ths_inode;
  37.     struct ths_sb_info *ths_sb;
  38.     struct ths_dir *dir;
  39.     long sektor=0,len,pos1;
  40.     int i,j;
  41.     unsigned char *data=NULL;
  42.  
  43.     ths_inode = (struct ths_inode_info *)&(in->u.generic_ip);
  44.     ths_sb = (struct ths_sb_info *)in->i_sb->u.generic_sbp;
  45.  
  46. #ifdef DEBUG
  47.     printk("readdir : %s : %ld\n",ths_inode->name,in->i_ino);
  48. #endif
  49.  
  50.     len = in->i_size;
  51.     pos1 = fp->f_pos;
  52. /*
  53.  * . und .. im Rootverzeichnis emulieren
  54.  */
  55.     if(in->i_ino==THS_ROOT_INODE)
  56.         pos1-=64;
  57.     if(pos1<0)
  58.     {
  59.         j=0;
  60.         put_fs_byte('.',j+eintrag->d_name);
  61.         j++;
  62.         if(pos1==-32)
  63.         {
  64.             put_fs_byte('.',j+eintrag->d_name);
  65.             j++;
  66.         }
  67.         if(pos1==-64)
  68.             put_fs_long(THS_ROOT_INODE,&eintrag->d_ino);
  69.         put_fs_byte(0,j+eintrag->d_name);
  70.         put_fs_word(j,&eintrag->d_reclen);
  71.         fp->f_pos+=32;
  72.         return j;
  73.     }
  74.  
  75.     while(pos1 < len)
  76.     {
  77.         if(in->i_ino==THS_ROOT_INODE)
  78.         {
  79.             sektor = (long)ths_sb->RootStart + (pos1>>9);
  80.             if(ths_read_sektor(in->i_sb,sektor, &tbf))
  81.             {
  82.                 printk("Fehler\n");
  83.                 return -1;
  84.             }
  85.         }
  86.         else
  87.         {
  88.             if(ths_read_cluster(in->i_sb,ths_inode->Cluster,pos1,&tbf))
  89.             {
  90.                 printk("Fehler\n");
  91.                 return -1;
  92.             }
  93.         }
  94.  
  95.         data = tbf.data[(pos1/512)%tbf.sektore];
  96.         dir = (struct ths_dir *)&(data[(pos1 & 0x01ff)]);
  97.  
  98. /*
  99.  * Test, ob der Eintrag geloescht wurde oder ob es sich
  100.  * um den Namen des Volumes handelt.
  101.  */
  102.         if(dir->name[0]==0xe5||(dir->attribut & 0x08))
  103.         {
  104.             fp->f_pos += 32;
  105.             pos1 += 32;
  106.             if(in->i_ino==THS_ROOT_INODE)
  107.                 ths_free_sektor(&tbf);
  108.             else
  109.                 ths_free_cluster(&tbf);
  110.             continue;
  111.         }
  112.  
  113. /*
  114.  * Daten an den User uebergeben
  115.  */
  116.         for(i=0,j=0;i<11;i++)
  117.         {
  118.             if(i==8 && dir->name[i]!=0x20)
  119.             {
  120.                 put_fs_byte('.',j+eintrag->d_name);
  121.                 j++;
  122.             }
  123.             if(dir->name[i]!=0x20)
  124.             {
  125.                 put_fs_byte(dir->name[i],j+eintrag->d_name);
  126.                 j++;
  127.             }
  128.         }
  129.  
  130.         put_fs_long(sektor * 16 + ((pos1 & 0x01ff) >>5),&eintrag->d_ino);
  131.         put_fs_byte(0,j+eintrag->d_name);
  132.         put_fs_word(j,&eintrag->d_reclen);
  133.  
  134.  
  135.         if(in->i_ino==THS_ROOT_INODE)
  136.             ths_free_sektor(&tbf);
  137.         else
  138.             ths_free_cluster(&tbf);
  139.         fp->f_pos += 32;
  140.         pos1 += 32;
  141.         return j;
  142.     }
  143.     return 0;
  144. }
  145.  
  146.  
  147. int ths_lookup(struct inode *in,const char *name, int l, struct inode **result)
  148. {
  149.     struct ths_inode_info *ths_inode;
  150.     struct ths_sb_info *ths_sb;
  151.     struct ths_dir *dir=NULL;
  152.     struct ths_buffer tbf;
  153.     long sektor,len,innr;
  154.     int addr,i,test;
  155.     unsigned char *data=NULL;
  156.  
  157.     sektor = 0;
  158.     addr = 0;
  159.  
  160.     ths_inode = (struct ths_inode_info *)&(in->u.generic_ip);
  161.     ths_sb = (struct ths_sb_info *)in->i_sb->u.generic_sbp;
  162.  
  163. #ifdef DEBUG
  164.     printk("lookup : %s laenge : %d\n",name,l);
  165. #endif
  166.  
  167.     len = in->i_size;
  168.  
  169.     i=0;
  170.  
  171.     while(i < len)
  172.     {
  173.         if((i & 0x1ff) ==0)
  174.         {
  175.             if(in->i_ino==THS_ROOT_INODE)
  176.             {
  177.                 sektor = (long)ths_inode->Cluster + (i>>9);
  178.                 if(ths_read_sektor(in->i_sb,sektor, &tbf))
  179.                 {
  180.                     printk("Fehler\n");
  181.                     return -1;
  182.                 }
  183.             }
  184.             else 
  185.             {
  186.                 if(!((i/512)%ths_sb->SektorenProCluster))
  187.                 {
  188.                     if(ths_read_cluster(in->i_sb,ths_inode->Cluster,i,&tbf))
  189.                     {
  190.                         printk("Fehler\n");
  191.                         return -1;
  192.                     }
  193.                 }
  194.             }    
  195.             data = tbf.data[(i/512)%tbf.sektore];
  196.         }
  197.         dir = (struct ths_dir *)&(data[(i & 0x01ff)]);
  198.  
  199. /*
  200.  * Namensvergleich
  201.  */
  202.         test=vergleich(dir,name,l);
  203.  
  204.         if(name[0]=='.' && l == 1 && in->i_ino == THS_ROOT_INODE)
  205.             test = 1;
  206.  
  207.         if(test)
  208.         {
  209.             innr = tbf.cluster ? (tbf.cluster*tbf.sektore+ths_sb->DatenStart)*16 + ((i&((0x100<<tbf.sektore)-1))>>5) :
  210.                                 tbf.sektnr[0]*16 + ((i&0x1ff)>>5);
  211.             if(l==1 && name[0] =='.')
  212.                 innr = in->i_ino;
  213.             if(l==2 && name[0] =='.' && name[1] =='.' && dir->cluster ==0)
  214.                 innr = THS_ROOT_INODE;
  215.             *result = iget(in->i_sb,innr);
  216.             if(!*result)
  217.             {
  218.                 iput(in);
  219.                 if(in->i_ino==THS_ROOT_INODE)
  220.                     ths_free_sektor(&tbf);
  221.                 else
  222.                     ths_free_cluster(&tbf);
  223.                 return -EACCES;
  224.             }
  225.             iput(in);
  226.             if(in->i_ino==THS_ROOT_INODE)
  227.                 ths_free_sektor(&tbf);
  228.             else
  229.                 ths_free_cluster(&tbf);
  230.             return 0;
  231.         }
  232.         i+=32;
  233.         if((i & 0x1ff) ==0)
  234.         {
  235.             if(in->i_ino==THS_ROOT_INODE)
  236.                 ths_free_sektor(&tbf);
  237.             else if(!((i/512)%tbf.sektore))
  238.                 ths_free_cluster(&tbf);
  239.         }
  240.     }
  241.     iput(in);
  242.     return -ENOENT;
  243. }
  244.